/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2009-2020 Rock Mao
* creator : Rock Mao
* created : 2009-9-21   20:29
* filename: LaborHashString.cpp
-----------------------------------------------------------------------------
*/


#include "LaborHashString.h"
#include <locale>
#include <iostream>
using namespace std;

void CLHashDecryptTable::string_hash(const wchar_t* str, sHashValue& hashValue)
{
	wchar_t *key     = (wchar_t *)str;
	unsigned long seed1_i  = 0x7FED7FED, seed2_i = 0xEEEEEEEE;
	unsigned long seed1_va = 0x7FED7FED, seed2_va = 0xEEEEEEEE;
	unsigned long seed1_vb = 0x7FED7FED, seed2_vb = 0xEEEEEEEE;

	hashValue.indexHash = ELFHash(str);
	hashValue.valueA    = RSHash(str);
	hashValue.valueB    = JSHash(str);
	return ;
}


bool CLHashDecryptTable::init()
{
	if(m_bDecryptTableInited) 
		return true;

	int dwSeed = 0x100001;
	int p2     = 0x2AAAAB;
	int dwHeight;
	int dwLow;
	for( int i = 0; i < 0x100; i++)
	{
		for(int j = 0; j<5; j++)
		{
			dwSeed   = (dwSeed*125+3)%p2;
			dwHeight = (dwSeed&0xFFFF)<<0x10;
			dwSeed   = (dwSeed*125+3)%p2;
			dwLow    = (dwSeed&0xFFFF);
			m_DecryptTable[j][i] = dwHeight | dwLow ;
		}
	}

	m_bDecryptTableInited = true;
	return true;
}

int  CLHashDecryptTable::value(eHashDecryptPage decryptPage,int index)
{
	return table(decryptPage)[index];
}

CLHashDecryptTable& CLHashDecryptTable::singleton()
{
	static CLHashDecryptTable codeTable;
	codeTable.init();
	return codeTable;
}
